package io.xiyou.rpcfx.utils;

import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName：ZookeeperUtils
 * @Author：西柚
 * @Date：2021/10/8 1:40 下午
 * @Versiion：1.0
 */
public class ZookeeperUtils {

    private static ZooKeeper zooKeeper = null;

    public static ZooKeeper connect() throws IOException, InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        //连接zk
        zooKeeper = new ZooKeeper("192.168.0.54:2181", 60000, watchedEvent -> {
            if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
                latch.countDown();
            }
        });
        //无连接阻塞
        latch.await();
        return zooKeeper;
    }

}
